package com.audaque.springboot.foshanupload.core.util;

import java.io.*;

public class ObjectInOutputStreamUtil {
    public static byte[] serializeObjectToByteArr(Object obj) throws IOException {
        // 创建一个字节输出流
        ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
        // 创建一个对象输出流
        ObjectOutputStream objOutStream = new ObjectOutputStream(byteOutStream);
        // 写入对象
        objOutStream.writeObject(obj);
        // 关闭对象输出流
        objOutStream.close();
        // 将字节数组转换为字符串
        byte[] bytes = byteOutStream.toByteArray();
        // 关闭字节输出流
        byteOutStream.close();
        return bytes;
    }


    public static Object byteArrToSerializeObject(byte[]  bytes) throws IOException, ClassNotFoundException {
        // 创建一个字节输入流
        ByteArrayInputStream byteInStream = new ByteArrayInputStream(bytes);
        // 创建一个对象输入流
        ObjectInputStream objInStream = new ObjectInputStream(byteInStream);
        // 读取对象
        Object o = objInStream.readObject();
        // 关闭对象输入流
        objInStream.close();

        // 关闭字节输入流
        byteInStream.close();
        return o;
    }





}
